import { Ref, ref, watch } from 'vue'

export const useVModel = <P extends object, K extends keyof P>(
  props: P,
  key: K,
  emit: Function
) => {
  const model: Ref<P[K]> = ref(props[key]) as any
  watch(
    () => props[key],
    (val) => {
      model.value = val
    }
  )
  watch(model, (val) => {
    emit(`update:${key}`, val)
  })
  return model
}
